// pages/answerChallenge/answerChallenge.js
import {
    shareOptions,
} from "../../utils/access";
import {
    jump,
    setStorage,
    getStorage
} from "../../utils/utils";
// 引入动画插件
import lottie from './lottie'
const app = getApp()

Page({

    /**
     * 页面的初始数据
     */
    data: {

    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

    },
    /**
     * 初始化加载动画
     */
    init() {
        if (this.inited) {
            return
        }
        wx.createSelectorQuery()
            .selectAll('#canvas')
            .node((res) => {
                if (res[0]) {
                    const canvas = res[0].node
                    const context = canvas.getContext('2d')
                    canvas.width = 300
                    canvas.height = 300
                    lottie.setup(canvas)
                    lottie.loadAnimation({
                        loop: true,
                        autoplay: true,
                        path: 'https://image-classify.daieco.com/kejian-quanzi/u7eCAOnJV8aqb2a29a5e61a8be9c312452a965edb263.json',
                        rendererSettings: {
                            context,
                        },
                    })
                    this.inited = true
                }
            })
            .exec()
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
        this.init()
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
 
    },

    // 跳转开始答题
    jumpTest() {
        if(this.loading) return
        this.loading = true
        if (!app.isLogin()) {
            setStorage('redirectPath', '/pages/answerChallenge/answerChallenge')
            app.login()
            this.loading = false
            return
        }
        jump('answerChallengeTest')
        this.loading = false
    },
    // 跳转排行榜
    jumpRank() {
        if(this.loading) return
        this.loading = true
        if (!app.isLogin()) {
            setStorage('redirectPath', '/pages/answerChallenge/answerChallenge')
            app.login()
            this.loading = false
            return
        }
        jump('answerChallengeRank')
        this.loading = false
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function (e) {
        const userId = getStorage('userId') || ''
        return shareOptions('default', e,'测测你能连续答对多少道题',`/pages/answerHome/answerHome?inviteData=${userId}`,'https://image-classify.daieco.com/kejian-quanzi/Xv62AenIIFglee2bce92f62c93292f8e432ded186af1.png')
    }
})